package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.mapper.UserMapper;
import com.example.kaogong.sys.pojo.Student;
import com.example.kaogong.sys.mapper.StudentMapper;
import com.example.kaogong.sys.pojo.Teacher;
import com.example.kaogong.sys.pojo.User;
import com.example.kaogong.sys.pojo.vo.StudentInfoVo;
import com.example.kaogong.sys.pojo.vo.TeacherInfoVo;
import com.example.kaogong.sys.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * <p>
 * 学生 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页获取所有学生信息
     * @param current 当前页
     * @param size 每页大小
     * @return
     */
    @Override
    public RespBean<Page<StudentInfoVo>> getAllStudentInfo(Long current, Long size) {
        try {
            Page<StudentInfoVo> studentInfoVoPage = studentMapper.getAllStudentInfo(new Page<>(current, size));
            return RespBean.success(studentInfoVoPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error();
    }

    /**
     * 根据学生编号删除一条学生信息
     * @param sid 学生编号
     * @return
     */
    @Override
    public RespBean deleteStudentBySid(Integer sid) {
        try {
            // 查询学生信息
            Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("sid", sid));
            if (student == null)
                return RespBean.error("删除学生信息失败，学生不存在");

            // 逻辑删除学生信息
            student.setDeleted(true);
            int update = studentMapper.update(student, new UpdateWrapper<Student>().eq("sid", sid));
            if (update < 1)
                return RespBean.error("删除学生信息失败");

            // 逻辑删除学生对应用户
            Integer delete = userMapper.deleteUserByUid(student.getUid());

            // 删除失败 回滚
            if (delete < 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return RespBean.error("删除教师对应用户信息失败");
            }

        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespBean.error("删除教师对应用户信息失败");
        }
        return RespBean.success("删除学生信息成功");
    }

    /**
     * 修改学生信息
     * @param student 学生对象
     * @return
     */
    @Override
    public RespBean updateStudentInfo(Student student) {
        try {
            student.setUid(null);
            student.setDeleted(null);
            int update = studentMapper.update(student, new UpdateWrapper<Student>().eq("sid", student.getSid()));
            if (update >= 1)
                return RespBean.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error();
    }
}
